package com.dao.impl;

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.dao.ClazzDao;
import com.dao.StudentDao;
import com.model.Paper;
import com.model.Student;
import com.util.CompareTime;

@Repository("StudentDao")
public class StudentDaoImpl implements StudentDao {
    @Resource
    private SessionFactory sessionFactory;
    @Resource
    private ClazzDao clazzDao;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public void addStu(Student student) {
    	Session session = sessionFactory.getCurrentSession();
    	String sno = student.getUsername();
    	String pwd = student.getPassword();
    	Student s = clazzDao.getStuBySNOPWD(sno,pwd);
    	if(s!=null){
    		student.setPassword(s.getPassword());
    	}
        //String sql = "insert into db_student(username , password , stuname , classid ) values('"+student.getUsername()+"','"+student.getPassword()+"','"+student.getStuname()+"','"+student.getClazz().getId()+"') ";
        //session.createSQLQuery(sql).executeUpdate();
        session.save(student);
    }

    @Override
    public List<Student> findByExample(Student stu) {

        Session session = sessionFactory.getCurrentSession();
        List<Student> results = session.createCriteria("com.model.Student").add(create(stu)).list();
        return results;

    }

    @Override
    public List<Paper> getStudentPapers(String id) {

        Session session = sessionFactory.getCurrentSession();
        String hql = "select p from Paper p ,Paperclass pclass , Student s where s.username='" + id + "' and s.clazz.id = pclass.clazz.id and pclass.paper.id=p.id";
        Query query = session.createQuery(hql);
        List querypage = query.list();

        CompareTime comparaTime = new CompareTime(); // 打印状态

        for (int i = 0; i < querypage.size(); i++) {
            Paper p = (Paper) querypage.get(i);

            if (p.getBegintime() != null && p.getEndtime() != null) {
                p.setStatus(comparaTime.compareToTime(p.getBegintime(), p.getEndtime()));
            } else {
                p.setStatus("forever");
            }
        }
        return querypage;
    }

    @Override
    public List showScore(int sid) {
        Session session = sessionFactory.getCurrentSession();
        String hql = "select clpro.paper.id, clpro.paper.papername, clpro.paper.isjava,sum(clpro.score) from ClazzProanswer clpro " +
        		"where UNIX_TIMESTAMP(clpro.paper.endtime) < UNIX_TIMESTAMP(NOW()) and clpro.student.id=" + sid
                + " group by clpro.paper.id ";
        Query q = session.createQuery(hql);
        return q.list();
    }

    @Override
    public Student editInfo(int sid ,  String sno, String pas) {
       /* Student stu = null;
        Session session = sessionFactory.getCurrentSession();
        stu = (Student) session.get(com.model.Student.class, sid);
        stu.setPassword(pas);*/
        Session session = sessionFactory.getCurrentSession();
        String sql = "update db_student set password = '"+pas+"' where username = '"+sno+"'  ";
        Query q  = session.createSQLQuery(sql);
        q.executeUpdate();
        
        return (Student) session.get(com.model.Student.class, sid);
    }

    @Override
    public List stuList() {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ");
        return null;
    }

}
